package com.ccnf.controller;

import com.ccnf.core.bean.Response;
import com.ccnf.model.ao.PassToken;
import com.ccnf.service.SaasService;
import com.ccnf.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户社交信息表 前端控制器
 * </p>
 *
 * @author developer
 * @since 2018-10-09
 */
@Controller
@RequestMapping("/")
@Api(value = "PublicController", description = "开放的相关API")
public class PublicController {
    @Autowired
    private SaasService saasService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private HttpServletRequest request;

    @ApiOperation("应用入口地址")
    @PassToken
    @GetMapping("public/entry")
    public String saasEntry() {
        return "redirect:" + saasService.saasLoginUrl(request);
    }

    @PassToken
    @GetMapping("public/callback")
    public String saasReturn(@RequestParam Map<String, String> requestParams) {
        return "redirect:" + saasService.handleSaasLoignReturn(requestParams);
    }

    @PassToken
    @GetMapping("public/error")
    @ResponseBody
    public Response error(String message) {
        return Response.successWithMessage(message);
    }

    @ApiOperation("获取token")
    @PassToken
    @GetMapping("public/token")
    @ResponseBody
    public String token(Long expoId, Long userId, String channelCode) {
        return tokenService.createTokenByJJwt(userId, expoId, channelCode);
    }

    @PassToken
    @GetMapping
    @ResponseBody
    public String origin() {
        return "/";
    }

    @PassToken
    @GetMapping("csrf")
    @ResponseBody
    public String csrf() {
        return "csrf";
    }
}